package databus.client.consumer;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.databus.client.*;
import studio.raptor.databus.command.Event;

import javax.jms.JMSException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.LockSupport;

/**
 * Created on 2017/5/25  <br>
 *
 * @author koalton
 */
public class AccountCenter {
    //消费事件AddOrder:100001
    DatabusConsumer consumer1 = null;
    //消费事件ModifyOrder:100002
    DatabusConsumer consumer2 = null;
    public static final SimpleDateFormat Y2MS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Logger log = LoggerFactory.getLogger(AccountCenter.class);
    //两次获取消息的最大时间间隔-60s
    public static final int MAX_WAIT_RECEIVE_GAP_TIME = 10;
    //两次获取消息的初始化时间间隔-1s
    public static final int INIT_RECEIVE_GAP_TIME = 1;
    //两次获取消息时间间隔增长步长 2s
    public static final int RECEIVE_GAP_TIME_STEP = 2;

    public AccountCenter() {
        try {
            //实例配置对象
            DatabusConfig config = new DatabusConfig("Account");
            //通过配置实例化builder对象
            DatabusConsumerBuilder consumerBuilder = new DatabusConsumerBuilder(config);
            //创建消费事件[100001-AddOrder]的消费者,此消费者自动ACK消息
            consumer1 = consumerBuilder.create("AddOrder", 100001);
            consumer2 = consumerBuilder.create("ModifyOrder",100002);
        } catch (JMSException e1) {
            log.info(e1.toString());
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            log.info(e.getMessage());
            e.printStackTrace();
        }
    }

    @Test
    public void receive() {
        if( null == consumer1 || null == consumer2){
            System.out.println("消费者初始化失败");
            return ;
        }
        try {
            int waitTime = INIT_RECEIVE_GAP_TIME;
            while (true) {
                Event event  = consumer1.receive(1 * 1000);
                if ( waitTime == MAX_WAIT_RECEIVE_GAP_TIME  ){
                    break;
                }
                if (event == null) {
                    System.out.println("At " + Y2MS.format(new Date()) + " receive:The Queue is empty");
                    LockSupport.parkUntil(System.currentTimeMillis() + waitTime * 1000);
                    waitTime += RECEIVE_GAP_TIME_STEP;
                    if( waitTime >  MAX_WAIT_RECEIVE_GAP_TIME ){
                        waitTime = MAX_WAIT_RECEIVE_GAP_TIME;
                    }
                    continue;
                }
                waitTime = 1 ;
                System.out.println("At " + Y2MS.format(new Date()) + " receive:" + eventCoreInfo(event));

            }
            while (true) {
                Event event  = consumer2.receive(1 * 1000);

                if (event == null) {
                    System.out.println("At " + Y2MS.format(new Date()) + " receive:The Queue is empty");
                    LockSupport.parkUntil(System.currentTimeMillis() + waitTime * 1000);
                    waitTime += RECEIVE_GAP_TIME_STEP;
                    if( waitTime >  MAX_WAIT_RECEIVE_GAP_TIME ){
                        waitTime = MAX_WAIT_RECEIVE_GAP_TIME;
                    }
                    continue;
                }
                waitTime = 1 ;
                System.out.println("At " + Y2MS.format(new Date()) + " receive:" + eventCoreInfo(event));

            }
        } catch (JMSException e) {
            log.info(e.getMessage());
            e.printStackTrace();
        }
    }

    public String eventCoreInfo(Event event) {
        //事件来源
        String eventSource = event.getEventBasicData().getSource();
        //事件序号
        String eventSeq = event.getEventBasicData().getKindId()+"";
        //事件名称
        String eventName = event.getEventBasicData().getName();
        //业务编号
        String businessNo = event.getEventMetaData().getBusinessNo()+"";
        //业务数据
        String centent = event.getEventBusinessData().getContent();
        return "{EventSource:" + eventSource + ",eventSeq:" + eventSeq + ",eventName:" + eventName + ",businessNo:" + businessNo + "}";
    }
}
